<script lang="ts" setup name="Goods">
import useStore from '@/store'
import { ref, watchEffect } from 'vue'
import { useRoute } from 'vue-router'
import GoodsImage from './components/goods-image.vue'
import GoodsSales from './components/goods-sales.vue'
import GoodsName from './components/goods-name.vue'
import GoodsSku from './components/goods-sku.vue'
import GoodsDetail from './components/goods-detail.vue'
import GoodsHot from './components/goods-hot.vue'
import Message from '@/components/message'
import { CartItem } from '@/types/cart'
const { goods, cart } = useStore()
const route = useRoute()
watchEffect(() => {
  if (route.params.id && route.path === `/goods/${route.params.id}`) {
    // 在获取下一次商品信息之前,将商品信息先清空
    goods.resetGoodsInfo()
    // 再获取新的商品信息
    goods.getGoodsInfo(route.params.id as string)
  }
})
let changedSkuId = ''
const selectedGoods = (skuId: string) => {
  // console.log(skuId)
  changedSkuId = skuId
  // 父组件接收到这个skuId, 要根据这个skuId 更新商品的信息
  const sku = goods.goodsInfo.skus.find(sku => sku.id === skuId)
  // console.log(sku)
  if (sku) {
    // 这个位置我们在直接修改仓库的数据 不够严谨, 严格来说应该提交仓库的一个action
    goods.goodsInfo.inventory = sku.inventory
    goods.goodsInfo.oldPrice = sku.oldPrice
    goods.goodsInfo.price = sku.price
  }
}

// 输入框计数器的值
const num = ref(1)
const addCart = async () => {
  // 选择了完整规格 才运行添加购物车, 如果没选完规格 提示
  if (!changedSkuId) return Message.warning('请选择完整规格')
  // 从goods.goodsInfo.skus中查找到sku对象, 将查找到的sku的specs循环展示处理成一个字符串
  const sku = goods.goodsInfo.skus.find(sku => sku.id === changedSkuId)
  const arr: string[] = []
  if (sku) {
    sku.specs.forEach(spec => {
      arr.push(`${spec.name}: ${spec.valueName}`)
    })
  }
  // 发送添加购物车的请求 携带 skuId和商品购买的数量
  await cart.addCart({
    // 本地添加
    id: goods.goodsInfo.id, // 商品的id
    name: goods.goodsInfo.name, // 商品的名字
    picture: goods.goodsInfo.mainPictures[0], // 商品图片
    price: goods.goodsInfo.price, // 商品价格
    count: num.value, // 购买商品的数量
    skuId: changedSkuId, // skuId
    attrsText: arr.join(' '), // 商品的规格描述
    selected: true, // 是否是选中状态
    nowPrice: goods.goodsInfo.price, // 商品现价
    stock: goods.goodsInfo.inventory, // 商品库存
    isEffective: true, // 商品是否有效
  } as CartItem)
  // 添加购物车成功的消息提示
  Message.success('添加购物车成功')

}
</script>
<template>
  <div class="xtx-goods-page">
    <div class="container">
      <!-- 面包屑 -->
      <XtxBread v-if="goods.goodsInfo.id">
        <XtxBreadItem to="/">首页</XtxBreadItem>
        <XtxBreadItem to="/">{{ goods.goodsInfo.categories[1].name }}</XtxBreadItem>
        <XtxBreadItem to="/">{{ goods.goodsInfo.categories[0].name }}</XtxBreadItem>
        <XtxBreadItem to="/">{{ goods.goodsInfo.name }}</XtxBreadItem>
      </XtxBread>
      <!-- 商品信息 -->
      <div class="goods-info" v-if="goods.goodsInfo.id">
        <!-- 左侧的放大镜区域 -->
        <div class="media">
          <GoodsImage :images="goods.goodsInfo.mainPictures"></GoodsImage>
          <!-- 销量人气/评价等 -->
          <GoodsSales />
        </div>
        <!-- 右侧商品描述 选择商品规格区域 -->
        <div class="spec">
          <!-- 商品名称+描述+城市选择 -->
          <GoodsName :goods="goods.goodsInfo"></GoodsName>
          <!-- sku组件 规格 -->
          <GoodsSku :goods="goods.goodsInfo" @selectedGoods="selectedGoods"></GoodsSku>
          <!-- 计数器组件 -->
          <!-- <XtxNumbox :num="num" @changeNum="changeNum"></XtxNumbox> -->
          <XtxNumbox v-model="num" :min="1" :max="10" label="数量"></XtxNumbox>
          <!-- 加入购物车按钮 -->
          <XtxButton type="primary" size="middle" style="margin-top: 20px" @click="addCart">加入购物车</XtxButton>
        </div>
      </div>
      <!-- 商品详情 -->
      <div class="goods-footer">
        <div class="goods-article">
          <!-- 商品+评价 -->
          <div class="goods-tabs">
            <GoodsDetail v-if="goods.goodsInfo.id" :goods="goods.goodsInfo"></GoodsDetail>
          </div>
        </div>
        <!-- 24热榜+专题推荐 -->
        <div class="goods-aside">
          <GoodsHot :type="3"></GoodsHot>
          <GoodsHot :type="2"></GoodsHot>
          <GoodsHot :type="1"></GoodsHot>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped lang="less">
.goods-info {
  min-height: 600px;
  background: #fff;
  display: flex;

  .media {
    width: 580px;
    height: 600px;
    padding: 30px 50px;
  }

  .spec {
    flex: 1;
    padding: 30px 30px 30px 0;
  }
}

.goods-info {
  min-height: 600px;
  background: #fff;
}

.goods-footer {
  display: flex;
  margin-top: 20px;

  .goods-article {
    width: 940px;
    margin-right: 20px;
  }

  .goods-aside {
    width: 280px;
    min-height: 1000px;
  }
}

.goods-tabs {
  min-height: 600px;
  background: #fff;
}

.goods-warn {
  min-height: 600px;
  background: #fff;
  margin-top: 20px;
}
</style>